import api from '../../api';
import { message } from 'antd';
import { forEach } from 'lodash';
import { useCallback } from 'react';

export default {
  namespace: 'workFlowM',
  state: {
    columns: undefined, //工作流查询列表表头
    dataSource: undefined, //工作流查询列表数据源
    pageInfo: {}, //工作流列表信息
    flowChildTypeList: [],
  },
  effects: {
    //查询工作流列表
    *queryFlowList({ payload }, { call, put }) {
      const response = yield call(api.workFlow.findFlowList, payload);
      if (response) {
        if (response.code == 0) {
          yield put({
            type: 'setState',
            payload: {
              columns: response.data.columns || [], //工作流查询列表表头
              dataSource: response.data.dataSource || [], //工作流查询列表数据源
              pageInfo: response.data.pageInfo || {}, //工作流列表信息
            },
          });
        } else {
          yield put({
            type: 'setState',
            payload: {
              columns: [], //工作流查询列表表头
              dataSource: [], //工作流查询列表数据源
              pageInfo: {}, //工作流列表信息
            },
          });
          message.error(response.msg || '查询工作流列表失败!');
        }
      } else {
        yield put({
          type: 'setState',
          payload: {
            columns: [], //工作流查询列表表头
            dataSource: [], //工作流查询列表数据源
            pageInfo: {}, //工作流列表信息
          },
        });
        message.error('查询工作流列表失败!');
      }
    },
    //删除某一条工作流
    *deleteFlowById({ payload, callback }, { call, put }) {
      const response = yield call(api.workFlow.deleteFlow, payload);
      const { type } = payload;
      if (response) {
        if (response.code == 0) {
          callback('success');
        } else {
          callback('fail');
          message.error(response.msg || '删除失败!');
        }
      } else {
        callback('fail');
        message.error('删除失败!');
      }
    },
    //新增工作流
    *addFlow({ payload, callback }, { call, put }) {
      const response = yield call(api.workFlow.addFlow, payload);
      if (response) {
        if (response.code == 0) {
          callback('success');
        } else {
          callback('fail');
          message.error(response.msg || '新增工作流失败!');
        }
      } else {
        callback('fail');
        message.error('新增工作流失败!');
      }
    },
    //编辑工作流
    *editFlow({ payload, callback }, { call, put }) {
      const response = yield call(api.workFlow.editFlow, payload);
      if (response) {
        if (response.code == 0) {
          callback('success');
        } else {
          callback('fail');
          message.error(response.msg || '修改工作流失败!');
        }
      } else {
        callback('fail');
        message.error('修改工作流失败!');
      }
    },
    //业务流程子类下拉列表
    *getFlowChildType({ payload, callback }, { call, put }) {
      const response = yield call(api.workFlow.getFlowChildType, payload);
      if (response) {
        if (response.code == 0) {
          yield put({
            type: 'setState',
            payload: {
              flowChildTypeList: response.data.dataSource || [], //查询业务流程子类
            },
          });
        } else {
          yield put({
            type: 'setState',
            payload: {
              flowChildTypeList: [], //查询业务流程子类
            },
          });
          message.error(response.msg || '获取业务流程子类下拉列表失败!');
        }
      } else {
        yield put({
          type: 'setState',
          payload: {
            flowChildTypeList: [], //查询业务流程子类
          },
        });
        message.error('获取业务流程子类下拉列表失败!');
      }
    },
  },

  reducers: {
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
